/**
 * 
 */
package com.pocketpets.service.attraction;

import java.util.ArrayList;
import java.util.Properties;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.gson.Gson;
import com.pocketpets.main.HttpJsonConnection;
import com.pocketpets.ui.map.AttractionSpot;

/**
 * @author leo
 * 
 */
public class JsonAttractionService implements IAttractionService {

	public String getAttractionBriefService(String AttractionID) {
		// JSONObject jBrief = null;
		String brief = null;
		Properties parameters = new Properties();
		parameters.setProperty("USERID", "");
		parameters.setProperty("CREDENTIAL", "");
		parameters.setProperty("ATTRACTIONID", AttractionID);
		String jBriefString = HttpJsonConnection.callService("attr_j", 106,
				parameters);
		try {
			// Log.i("JJJJJJJJJJJJJ", jBriefString);
			// String temp=jBriefString;
			// jBriefString=temp.substring(temp.indexOf("(")+1,temp.indexOf(")")+1);
			JSONObject jObject = new JSONObject(jBriefString);
			// JSONObject jObject=new JSONObject("aaa"+jBriefString+"");
			brief = jObject.getString("brief");
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		return brief;
	}

	public ArrayList<AttractionSpot> getLocationService(int layer, double MinX,
			double MinY, double MaxX, double MaxY) {
		ArrayList<AttractionSpot> aspot = new ArrayList<AttractionSpot>();
		JSONObject jCombindAttr = null;
		JSONArray jArray = null;
		JSONObject jLocationObject = null;
		layer = 15;
		Properties parameters = new Properties();
		parameters.setProperty("USERID", "");
		parameters.setProperty("CREDENTIAL", "");
		parameters.setProperty("LAYER", String.valueOf(layer));
		parameters.setProperty("MINX", String.valueOf(MinX));
		parameters.setProperty("MINY", String.valueOf(MinY));
		parameters.setProperty("MAXX", String.valueOf(MaxX));
		parameters.setProperty("MAXY", String.valueOf(MaxY));
		Log.i("LAYER", String.valueOf(layer));
		Log.i("Minx", String.valueOf(MinX));
		Log.i("Miny", String.valueOf(MinY));
		Log.i("Maxx", String.valueOf(MaxX));
		Log.i("Maxy", String.valueOf(MaxY));

		String jAttractionString = HttpJsonConnection.callService("attr_j",
				101, parameters).toString();

		try {
			Log.i("SSSSSSSSSSSSSSSSSS", jAttractionString);
			// jAttractionString="[ {locationId:\"attr11719\", locationName:\"\\u4e2d\\u534e\\u4e66\\u5c40\", lat:114.21425, lng:22.285529, type:\"0\" },{locationId:\"C000008466\", locationName:\"\\u4e09\\u8054\\u4e66\\u5e97...\", lat:114.15541648865, lng:22.286277066071, type:\"1\" },{locationId:\"C000008472\", locationName:\"\\u6148\\u4e91\\u5c71\\u89c2\\u97f3\\u5e99...\", lat:114.20039176941, lng:22.352774798311, type:\"1\" }]";

			jArray = new JSONArray(jAttractionString);

			// Log.i("SSSSSSSSSS",jArray.get(1).toString());
			// Log.i("SSSSSSSSSS",jArray.toString());
			// JSONArray jnameArray=jCombindAttr.names();
			// JSONArray jvalArray=jCombindAttr.toJSONArray(jnameArray);
			// String[] attID=new String[3];
			// for (int i=0; i<jvalArray.length();i++)
			for (int i = 0; i < jArray.length(); i++) {
				jLocationObject = jArray.getJSONObject(i);
				// jCombindAttr=jArray.getJSONObject(i);
				// jLocationObject=jCombindAttr.getJSONObject("location");
				GeoPoint point = new GeoPoint(
						(int) (jLocationObject.getDouble("lng") * 1e6),
						(int) (jLocationObject.getDouble("lat") * 1e6));
				// GeoPoint point = new
				// GeoPoint(JsonAttractionService.getAttraction(MinX, MinY,
				// MaxX, MaxY));
				// AttractionSpot overlayitem = new AttractionSpot(point,
				// jLocationObject.getString("locationName"),getAttractionBrief(jLocationObject.getString("locationID"))
				// );
				AttractionSpot overlayitem = new AttractionSpot(point,
						jLocationObject.getString("name"),
						jLocationObject.getString("cid"),
						jLocationObject.getInt("count"));
				// attID[i]=jvalArray.getJSONObject(i).toString();
				aspot.add(overlayitem);
			}

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return aspot;
	}

	public String getGsonAttractionService(int layer, String cid) {
		Gson gson = new Gson();
		Properties parameters = new Properties();
		parameters.setProperty("USERID", "");
		parameters.setProperty("CREDENTIAL", "");
		parameters.setProperty("LAYER", String.valueOf(layer));
		parameters.setProperty("CID", cid);
		parameters.setProperty("PAGE", "0");
		String jAttractionString = HttpJsonConnection.callService("attr_j",
				117, parameters).toString();
		return jAttractionString;
	}
	/*
	 * @Override public ArrayList<JSONObject> getAttractionService(int layer,
	 * String cid) { // TODO Auto-generated method stub ArrayList<JSONObject>
	 * aAttractionObject=new ArrayList<JSONObject>(); Properties parameters =
	 * new Properties(); parameters.setProperty("USERID", "");
	 * parameters.setProperty("CREDENTIAL", "");
	 * parameters.setProperty("LAYER",String.valueOf(layer));
	 * parameters.setProperty("CID",cid); parameters.setProperty("PAGE","0");
	 * String jAttractionString = HttpJsonConnection.callService("attr_j", 117,
	 * parameters).toString(); JSONArray jArray; try { jArray = new
	 * JSONArray(jAttractionString); for (int i=0; i<jArray.length();i++) {
	 * aAttractionObject.add(jArray.getJSONObject(i)); } } catch (JSONException
	 * e) { // TODO Auto-generated catch block e.printStackTrace(); }
	 * 
	 * return aAttractionObject; }
	 */

}
